<?php

namespace addons\shopro\controller\wm;

use addons\shopro\exception\Exception;
use think\Db;
use addons\shopro\controller\Base as AddonsBase;
class Group extends AddonsBase
{

    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];


    /**
     * 团长个人中心 资金统计
     * 王猛 2022-2-8 15:53:16
     */
    function Index(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            $income = db('shopro_user_wallet_log')
                ->alias('a')
                ->join('shopro_order_item b','a.item_id=b.id')
                ->where(['a.user_id'=>$this->auth->id,'a.wm_types'=>'pt','a.type'=>'xt','a.wallet_type'=>'money'])
                ->field('a.id as id,b.activity_type,b.goods_title,b.goods_num,b.goods_sku_text,b.goods_image,b.goods_price,a.wallet,a.ext,a.memo,a.createtime,b.createtime as orderCreatetime,b.money_pt,b.money_pt3,b.money_pt2,b.money_pt1,b.money_sys,b.money_sj,b.domoneytime,b.bind_domoneytime,b.pt_domoneytime')
                ->sum('wallet');
            $data = [
                'income'=>$income,
            ];

            $user_info = Db::name('user')->where(['id'=>$this->auth->id])->find();
            $user_info['head_introduction_images_array'] = empty($user_info['head_introduction_images'])? []:explode(',',$user_info['head_introduction_images']);
            foreach ($user_info['head_introduction_images_array'] as &$head_introduction_image){
                $head_introduction_image = empty($head_introduction_image)? '':cdnurl($head_introduction_image,true);
            }
            unset($head_introduction_image);

            $data['head_introduction'] = $user_info['head_introduction'];
            $data['head_introduction_images'] = $user_info['head_introduction_images'];
            $data['head_introduction_images_array'] = $user_info['head_introduction_images_array'];
            $data['head_service_commitment'] = $user_info['head_service_commitment'];
            $data['head_label'] = $user_info['head_label'];

            $this->success('',$data);
        }
    }


    /**
     * 店长、团长佣金流水
     */
    function groupOrder(){
        $params = $this->request->post();

        $types = isset($params['types']) ? $params['types'] : 'pt';
        $date = isset($params['date']) ? explode('-', $params['date']) : [];
        $start = isset($date[0]) && $date[0] ? strtotime($date[0]) : strtotime(date('Y-m') . '-01');
        $end = isset($date[1]) && $date[1] ? (strtotime($date[1]) + 86399) : strtotime(date('Y-m-d')) + 86399;

        $pagesize = 10;
        $page = $params['page']??1;
        $user_id = $this->auth->id;
        $lists = db('shopro_user_wallet_log')
            ->alias('a')
            ->join('shopro_order_item b','a.item_id=b.id')
            ->where(['a.user_id'=>$user_id,'a.wm_types'=>$types,'a.type'=>'xt','a.wallet_type'=>'money'])
            ->whereBetween('a.createtime', [$start, $end])
            ->field('a.id as id,b.activity_type,b.goods_title,b.goods_num,b.goods_sku_text,b.goods_image,b.goods_price,a.wallet,a.ext,a.memo,a.createtime,b.createtime as orderCreatetime,b.money_pt,b.money_pt3,b.money_pt2,b.money_pt1,b.money_sys,b.money_sj,b.domoneytime,b.bind_domoneytime,b.pt_domoneytime')
            ->paginate()->each(function($item, $key){
                $item['order_info'] = json_decode($item['ext'],true);
                if($item['activity_type']=='groupon') $item['status_name']='拼团订单';
                if($item['activity_type']=='seckill') $item['status_name']='积分订单';
                if($item['activity_type']=='' || $item['activity_type']==NULL) $item['status_name']='常规订单';
                return $item;
            });

        $income = db('shopro_user_wallet_log')
            ->alias('a')
            ->join('shopro_order_item b','a.item_id=b.id')
            ->where(['a.user_id'=>$user_id,'a.wm_types'=>$types,'a.type'=>'xt','a.wallet_type'=>'money'])
            ->whereBetween('a.createtime', [$start, $end])
            ->field('a.id as id,b.activity_type,b.goods_title,b.goods_num,b.goods_sku_text,b.goods_image,b.goods_price,a.wallet,a.ext,a.memo,a.createtime,b.createtime as orderCreatetime,b.money_pt,b.money_pt3,b.money_pt2,b.money_pt1,b.money_sys,b.money_sj,b.domoneytime,b.bind_domoneytime,b.pt_domoneytime')
            ->sum('wallet');

        $res = [
            'lists'=>$lists,
            'wm_types'=>$types,
            'income'=>$income
        ];
        $this->success('',$res);
    }


}